package com.it.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.CommentService;
import com.it.bean.ConstEnum;
import com.it.dao.CommentMapper;
import com.it.dao.UserMapper;
import com.it.pojo.Comment;
import com.it.pojo.User;
import com.it.pojo.vo.comment.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;


    @Resource
    private UserMapper userMapper;

    @Override
    public int insertComment(Comment comment) {
        Date date = new Date();
        if (comment.getParentCommentId() == 0 || comment.getParentUserId() == 0) {
            comment.setLevel(1);
        } else {
//            if (comment.getr)
            comment.setLevel(2);
        }
        comment.setGmtCreate(date);
        comment.setGmtModified(date);
//        comment.set
        return commentMapper.insert(comment);
    }

    @Override
    public int deleteCommentById(Integer commentId) {
        return commentMapper.deleteById(commentId);

    }

    @Override
    public Map<String, Object> listComments(Integer articleId, Integer start, Integer limit) {
//        LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
//        wrapper.orderByDesc(Comment::getGmtCreate);
//        List<Comment> commentList = commentMapper.selectList(wrapper);
//        List<CommentVO> collect = commentList.stream().map(comment -> {
//            CommentVO commentVO = new CommentVO();
//            BeanUtils.copyProperties(comment, commentVO);
//            return commentVO;
//        }).collect(Collectors.toList());
        Map<String, Object> map1 = new HashMap<>();
        System.out.println("articleId = " + articleId);
        List<CommentVO> collect = commentMapper.selectListByArticleId(articleId);

        Map<Integer, List<CommentVO>> map = collect.stream().collect(Collectors.groupingBy(CommentVO::getParentCommentId));
        collect.forEach(e -> {
            e.setChildren(map.get(e.getCommentId()));
        });
        List<CommentVO> list = collect.stream().filter(commentVO -> commentVO.getLevel() == Integer.parseInt(ConstEnum.FIRST_LEVEL.getResult()))
                .map(commentVO -> {
                    if (commentVO.getChildren() != null) { // 判空，防止直接评论没子评论导致下面遍历空指针
                        commentVO.getChildren().forEach(e -> {
                            if (e.getParentCommentId() != e.getReplyCommentId()) {  // 直接回复不添加@
                                if (e.getLevel() == 2) {
                                    Integer replyCommentUserId = e.getReplyCommentUserId();
                                    User user = userMapper.selectById(replyCommentUserId);
                                    if (user != null) {
                                        String nickname = e.getNickname();
                                        e.setNickname(nickname + "<span>回复 </span><span style='color:#1ECE9B'> @ </span> " + user.getNickname());
                                    }
                                }
                            }
                        });
                    }
                    return commentVO;
                }).collect(Collectors.toList());
        Integer total = list.size();
        List<CommentVO> list1 = list.stream().skip(start).limit(limit).collect(Collectors.toList());
        map1.put("total", total);
        map1.put("commentList", list1);
        return map1;
    }
}
